<?php
namespace Admin\Controller;
use Think\Controller;
class PhotoController extends CommonController {

	/**
	 * [index 用户上传相片列表]
	 */
	public function index(){
		$this -> assign('test','照片管理');
		$this -> display();
	}

	/**
	 * [ajaxphoto 处理ajax, 返回图片地址]
	 */
	public function ajaxphoto(){

		if (IS_AJAX) {

			$photos = M('photos');
			$where['if_check'] = 0;
			// 查询图片id和地址, 返回给模板展示
			$photos_list = $photos->field('photos_id,photos_path')->where($where)->limit('6')->select();

			if ($photos_list) {
				// 查到数据分配给模板前,  把if_check的字段更新为1,表示看过
				$photos_id = '';
				foreach ($photos_list as $val) {
					$photos_id .= $val['photos_id'].',';
				}
				$photos_id = rtrim($photos_id, ',');

				$data['photos_id'] = array('in', $photos_id);
				$photos->where($data)->save(array('if_check'=>'1'));

				$this->ajaxReturn($photos_list);
			} else {
				$this->ajaxReturn(false);
			}
		} else {
			E('页面不存在');
		}
	}

	/**
	 * [delphoto 处理ajax 删除图片]
	 */
	public function delphoto(){
		if (IS_AJAX) {

			$c = M();
			$c->startTrans();

			$photos_id = I('post.photos_id');
			$photos = M('photos');
			// $info = $photo->find($photes_id);
			$info = $photos->alias('p')->join("zz_albums a on a.albums_id=p.albums_id")->where("p.photos_id='$photos_id'")->field('p.time public_time, a.user_id')->select();


			$res = $photos->delete($photos_id);
			// $res = true;
			if ($res && $info) {
				$where['public_time'] = $info[0]['public_time'];
				$where['del_time'] = time();
				$where['user_id'] = $info[0]['user_id'];
				$where['cont_type'] = 2;
				$where['if_read'] = 1;
				$check = M('check');
				$id = $check->add($where);
				if ($id) {
					$c->commit();
					$this->ajaxReturn('1');
				} else {

					$c->rollback();
					$this->ajaxReturn('0');
				}

			} else {
				$c->rollback();
				$this->ajaxReturn('0');
			}



		} else {
			E('访问页面不存在');
		}
	}


}
